<template>
  <a-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    width="850px"
    :cancelText="$t('关闭')"
    :confirmLoading="confirmLoading"
  >
    <template slot="footer">
      <a-row :gutter="24">
        <a-col :span="12">
          <div style="text-align: left" v-if="!dataForm.id">
            <a-checkbox v-model="isContinue" @change="changeAddState"></a-checkbox> {{ $t('连续添加') }}
          </div>
        </a-col>
        <a-col :span="12">
          <a-button @click="handleCancel" :disabled="confirmLoading">{{ $t('取消') }}</a-button>
          <a-button type="primary" @click="handleOk" v-if="!disable" :disabled="confirmLoading">{{
            $t('保存')
          }}</a-button>
        </a-col>
      </a-row>
    </template>
    <div style="height: 500px; overflow-y: auto;overflow-x:hidden">
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model
            ref="dataForm"
            :rules="rules"
            :model="dataForm"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            v-enterToNext
          >
            <a-row>
              <div style="margin: 0 0 10px -10px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('基本信息') }}
              </div>
              <a-col :span="12">
                <a-form-model-item prop="code" style="margin-bottom: 10px" :label="$t('商品编码')">
                  <a-input v-model="dataForm.code" ref="code" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="name" style="margin-bottom: 10px" :label="$t('商品名称')">
                  <a-input v-model="dataForm.name" :disabled="disable"></a-input>
                </a-form-model-item>
                <a-form-model-item prop="kind" style="margin-bottom: 10px" :label="$t('商品分类')">
                  <a-tree-select
                    show-search
                    :filterTreeNode="filterTreeNode"
                    :disabled="disable"
                    allowClear
                    v-model="dataForm.kind"
                    :dropdownStyle="{ maxHeight: '250px', overflow: 'auto' }"
                    :tree-data="treeData"
                    :placeholder="$t('请选择')"
                    :getPopupContainer="triggerNode => { return triggerNode.parentNode || document.body }"
                  />
                </a-form-model-item>
                <a-form-model-item prop="cost" style="margin-bottom: 10px" :label="$t('参考成本')">
                  <a-input-number v-model="dataForm.cost" :disabled="disable"

                                  :formatter="formatter"
                                  :parser="parser"
                                  :precision="2"
                                  style="width: 100%;"></a-input-number>
                </a-form-model-item>
                <a-form-model-item prop="realCoin" style="margin-bottom: 10px" :label="$t('代币')">
                  <a-input-number v-model="dataForm.realCoin" :disabled="disable"

                                  :formatter="formatter"
                                  :parser="parser"
                                  style="width: 100%;"@change="changeSingleCoin"></a-input-number>
                </a-form-model-item>
                <a-form-model-item prop="presentCoin" style="margin-bottom: 10px" :label="$t('赠送代币')">
                  <a-input-number v-model="dataForm.presentCoin" :disabled="disable"

                           :formatter="formatter"
                                  :parser="parser"

                           style="width: 100%;"
                           @change="changeSingleCoin"></a-input-number>
                </a-form-model-item>
              </a-col>
              <a-col :span="12">
                <a-col :span="24" style="padding-right: 0">
                  <a-form-model-item
                    prop="picture"
                    :label="$t('图片')"
                    :labelCol="{ span: 3 }"
                    :wrapperCol="{ span: 20 }"
                  >
                    <j-upload v-model="dataForm.picture" fileType="image" :disabled="disable"></j-upload>
                  </a-form-model-item>
                </a-col>
              </a-col>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('销售兑换设置') }}
              </div>
              <a-col :span="24">
                <a-row style="margin-bottom:10px">
                  <a-col :span="6">
                    <a-checkbox v-model="dataForm.allowMemberDiscounts" :disabled="disable">{{
                      $t('享受会员优惠')
                    }}</a-checkbox>
                  </a-col>
                  <a-col :span="6" v-if="taxSetting == 1">
                    <a-checkbox :checked="dataForm.allowTax == 1" :disabled="disable" @change="(e) => changeSelectState(e, 'allowTax', 1)">{{ $t('销售加税') }}</a-checkbox>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="24">
                    <a-row>
                      <a-col :span="6" style="margin-top: 10px">
                        <a-checkbox v-model="dataForm.allowSales" :disabled="disable">{{ $t('允许销售') }}</a-checkbox>
                      </a-col>
                      <a-col :span="6">
                        <a-form-model-item prop="price" style="margin-bottom: 5px" :label="$t('售价')">
                          <a-input-number v-model="dataForm.price" :disabled="disable"

                                          :formatter="formatter"
                                  :parser="parser"
                                          :precision="2"
                                          style="width: 100%;"
                                          @change="changePrice"></a-input-number>
                        </a-form-model-item>
                      </a-col>
                      <a-col :span="6" style="margin-top: 10px"> {{ dataForm.singleCoin }}{{ $t('元/币') }} </a-col>
                      <a-col :span="6" style="margin-top: 10px" v-if="taxSetting == 2">
                        {{ $t('其中税费') }}：{{ dataForm.taxPrice }}
                      </a-col>
                    </a-row>
                  </a-col>
                </a-row>
              </a-col>
              <a-col :span="12">
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowLotterys" style="margin-bottom: 5px" label="" label-width="50px">
                      <a-checkbox v-model="dataForm.allowLotterys" ref="allowLotterys" :disabled="disable">{{
                        $t('允许彩票兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="lottery" :label="$t('彩票')" style="margin-bottom: 5px">
                      <a-input-number v-model="dataForm.lottery" ref="lottery" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowPointss" style="margin-bottom: 5px">
                      <a-checkbox
                        v-model="dataForm.allowPointss"
                        ref="allowPointss"
                        :disabled="disable"
                        style="width: 110%"
                        >{{ $t('允许A币兑换') }}</a-checkbox
                      >
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="points" style="margin-bottom: 5px" :label="$t('A币')">
                      <a-input-number v-model="dataForm.points" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"
                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
              <a-col :span="12">
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item prop="allowIntegrals" style="margin-bottom: 5px">
                      <a-checkbox v-model="dataForm.allowIntegrals" :disabled="disable">{{
                        $t('允许积分兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="integral" style="margin-bottom: 5px" :label="$t('积分')">
                      <a-input-number v-model="dataForm.integral" ref="integral" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"

                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                </a-row>
                <a-row>
                  <a-col :span="12">
                    <a-form-model-item
                      prop="allowAlotterys"
                      :labelCol="{ span: 10 }"
                      :wrapperCol="{ span: 16 }"
                      style="margin-bottom: 5px"
                    >
                      <a-checkbox v-model="dataForm.allowAlotterys" ref="allowAlotterys" :disabled="disable">{{
                        $t('允许A票兑换')
                      }}</a-checkbox>
                    </a-form-model-item>
                  </a-col>
                  <a-col :span="12">
                    <a-form-model-item prop="alottery" style="margin-bottom: 5px" :label="$t('A票')">
                      <a-input-number v-model="dataForm.alottery" ref="alottery" :disabled="disable"

                                      :formatter="formatter"
                                  :parser="parser"

                                      style="width: 100%;"
                      ></a-input-number>
                    </a-form-model-item>
                  </a-col>
                </a-row>
              </a-col>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 10px 0 10px -30px; color: black; font-weight: 500; font-size: 15px">
                {{ $t('更多设置') }}
              </div>
              <a-row>
                <a-col :span="9">
                  <a-form-model-item prop="state" style="margin-bottom: 5px" :label="$t('上架状态')">
                    <a-radio-group v-model="dataForm.state" ref="state" :disabled="disable">
                      <a-radio value="Y">{{ $t('上架') }}</a-radio>
                      <a-radio value="N">{{ $t('下架') }}</a-radio>
                    </a-radio-group>
                  </a-form-model-item>
                </a-col>
                <a-col :span="9">
                  <a-form-model-item style="margin-bottom: 5px">
                    <a-checkbox v-model="dataForm.authSales" :disabled="disable">{{
                      $t('授权销售')
                    }}</a-checkbox>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="9">
                  <a-form-model-item prop="limitCycle" style="margin-bottom: 5px" :label="$t('限购类型')">
                    <j-dict-select-tag
                      :placeholder="$t('请选择')"
                      style="width: 100px"
                      v-model="dataForm.limitCycle"
                      @change="changelimitCycleState"
                      :disabled="disable"
                      dictCode="bis_goods_infor.limit_cycle"
                    />
                  </a-form-model-item>
                </a-col>
                <a-col :span="7" style="margin-left: -110px">
                  <a-form-model-item
                    prop="limitNum"
                    :labelCol="{ span: 7 }"
                    :wrapperCol="{ span: 16 }"
                    style="margin-bottom: 0px"
                    :label="$t('限购数量')"
                  >
                    <a-input v-model="dataForm.limitNum" :disabled="limitCycledisable"></a-input>
                  </a-form-model-item>
                </a-col>
                <a-col :span="10">
                  <div style="color: #f6aa64; margin-top: 5px">
                    {{ $t('(每个会员在特定的限购周期内最大允许的购买数量)') }}
                  </div>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowMemberType"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('适用会员')"
                  >
                    <a-radio-group
                      v-model="allowMemberType"
                      @change="getTypeValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定会员') }}</a-radio> </a-radio-group
                    ><br />
                    <a-checkbox-group
                      :span="24"
                      v-if="memberShow"
                      v-model="dataForm.allowMemberTypes"
                      style="margin-top: 6px; margin-left: 5px"
                    >
                      <a-checkbox style="margin-left: 8px" :value="item.id" v-for="item in memberList" :key="item.id">{{
                        item.name
                      }}</a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowDeviceType"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('适用渠道')"
                  >
                    <a-radio-group
                      v-model="allowDeviceType"
                      @change="getDeviceTypeValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定渠道') }}</a-radio>
                    </a-radio-group>
                    <br />
                    <a-checkbox-group
                      v-if="deviceTypeShow"
                      v-model="dataForm.allowDeviceTypes"
                      style="margin-top: 6px; margin-left: 5px"
                      :disabled="disable"
                      @change="getDeviceList"
                    >
                      <a-checkbox
                        style="margin-left: 8px"
                        :value="item.type"
                        v-for="item in deviceTypeList"
                        :key="item.type"
                        >{{ item.type_dictText }}
                      </a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col>
                  <a-form-model-item
                    prop="allowDevice"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-bottom: 5px; margin-left: 4px"
                    :label="$t('销售终端')"
                  >
                    <a-radio-group
                      v-model="allowDevice"
                      @change="getDeviceValue"
                      :disabled="disable"
                      style="margin-left: 5px"
                    >
                      <a-radio value="">{{ $t('全部') }}</a-radio>
                      <a-radio value="part">{{ $t('指定终端') }}</a-radio>
                    </a-radio-group>
                    <br />
                    <a-checkbox-group
                      v-if="deviceShow"
                      v-model="dataForm.allowDevices"
                      style="margin-top: 6px; margin-left: 5px"
                      :disabled="disable"
                    >
                      <a-checkbox style="margin-left: 8px" :value="item.id" v-for="item in deviceList" :key="item.id"
                        >{{ item.name }}
                      </a-checkbox>
                    </a-checkbox-group>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    prop="comment"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    :label="$t('商品简介')"
                    style="margin-left: 4px"
                  >
                    <a-textarea v-model="dataForm.comment" ref="comment" :rows="2" :disabled="disable" />
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-row>
                <a-col :span="24">
                  <a-form-model-item
                    prop="details"
                    :label="$t('商品详情')"
                    :labelCol="{ span: 2 }"
                    :wrapperCol="{ span: 20 }"
                    style="margin-left: 4px"
                  >
                    <div style="color: #e28909">
                      {{
                        $t('用于小程序商品详情的展示；建议图片宽度375px，上传多张图片时，每张图片的宽度应该保持一致。')
                      }}
                    </div>
                    <j-upload
                      v-model="dataForm.details"
                      fileType="image"
                      :disabled="disable"
                      class="detail-upload"
                      text="upload"
                    ></j-upload>
                  </a-form-model-item>
                </a-col>
              </a-row>
            </a-row>
            <a-row style="margin-left: 20px">
              <div style="margin: 0 0 10px -30px">
                <a-checkbox
                  style="padding-top: 6px; color: black; font-weight: 500; font-size: 15px"
                  :checked="chargeAfter"
                  @change="changeChargeStatue"
                  >{{ $t('充值后晋级') }}</a-checkbox
                >
              </div>
              <a-col :span="24">
                <div>
                  <div style="margin-bottom: 10px" v-for="(item, index) in elevation" :key="index">
                    <a-select
                      :placeholder="$t('请选择晋级前会员类型')"
                      v-model="item.memberType"
                      style="width: 200px; margin-left: 8px"
                      :disabled="item.disable"
                    >
                      <a-select-option v-for="level in levelList" :key="level.id" :value="level.id">
                        <span>{{ level.level }}{{ level.name }}</span>
                      </a-select-option>
                    </a-select>
                    <span style="margin-left: 8px">{{ $t('晋级到') }}</span>
                    <a-select
                      :placeholder="$t('请选择晋级后会员类型')"
                      v-model="item.memberTypeAfter"
                      style="width: 200px; margin-left: 8px"
                      :disabled="item.disable"
                    >
                      <a-select-option v-for="level in levelList" :key="level.id" :value="level.id">
                        <span>{{ level.level }}{{ level.name }}</span>
                      </a-select-option>
                    </a-select>
                    <a href="javascript:void(0);" style="margin-left: 8px" v-if="item.disable"
                      ><a-icon type="edit" @click="editElevation(index)"
                    /></a>
                    <a href="javascript:void(0);" style="margin-left: 8px" v-else
                      ><a-icon type="check" @click="saveElevation(item, index)"
                    /></a>
                    <a href="javascript:void(0);" style="margin-left: 8px"
                      ><a-icon type="close" @click="deleteElevation(index)"
                    /></a>
                  </div>
                  <a href="javascript:void(0);" @click="addElevation" style="line-height: 32px; margin-left: 10px">{{
                    $t('添加')
                  }}</a>
                </div>
              </a-col>
              <div style="margin: 0 0 10px -30px">
                <a-checkbox
                  style="padding-top: 6px;"
                  :checked="renewalAfter"
                  @change="changeRenewalStatue"
                  >{{ $t('充值后续期') }}</a-checkbox
                >
                <span v-for="(item, index) in renewalList" :key="index">
                  <j-dict-select-tag
                    :placeholder="$t('请选择')"
                    style="width: 100px"
                    v-model="item.type"
                    :disabled="disable"
                    dictCode="bis_member_rule_renewal.type"
                  />
                  <span style="margin-left: 8px">{{ $t('开始续期') }}</span>
                  <a-input v-model="item.renewal" style="width:140px;margin-left: 8px"></a-input>
                  <span style="margin-left: 8px">{{ $t('月') }}</span>
                </span>
              </div>
              <div style="margin: 0 0 10px -30px">
                <a-checkbox
                  style="padding-top: 6px;"
                  v-model="dataForm.freeWithRecharge"
                  >{{ $t('开卡直接购买免收押金') }}</a-checkbox
                >
              </div>
            </a-row>
          </a-form-model>
        </a-col>
      </a-row>
    </div>
  </a-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { initDictOptions } from '@/components/dict/JDictSelectUtil'
import {formatter, parser} from "@api/comm";

export default {
  name: 'TicketsModal',
  components: {
  },
  data() {
    return {
      formatter,
      parser,
      form: this.$form.createForm(this),
      title: '',
      confirmLoading: false,
      dataForm: {
        code: '',
        name: '',
        warnNum: 0,
        verifyType: '1',
        limitCycle: '0',
        cost: 0,
        allowMemberDiscount: 'Y',
        allowSale: 'Y',
        authSale: '',
        authSales: false,
        state: 'Y',
        allowDevices: [],
        allowDevice: '',
        allowDeviceTypes: [],
        allowDeviceType: '',
        form: 2,
        presentCoin: '0',
        realCoin: '0',
        species: '1',
        freeWithRecharge: false
      },
      treeData: [],
      deviceShow: false,
      deviceTypeShow: false,
      memberShow: false,
      visible: false,
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 15 },
      },
      rules: {
        supplierId: [{ required: true, message: this.$t('请选择所属商户') }],
        name: [{ required: true, message: this.$t('请输入卖品名称') }],
        kind: [{ required: true, message: this.$t('请选择卖品分类') }],
        cost: [
          { required: true, message: this.$t('请输入卖品参考成本') },
          { pattern: /^\d+.?\d{0,2}$/, message: this.$t('小数点后最多输入两位') },
        ],
        realCoin: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        presentCoin: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
        lottery: [{ pattern: /^[+]{0,1}(\d+)$/, message: this.$t('请输入0或正整数') }],
      },
      dataSource: [],
      pagination: false,
      recordsAmount: '',
      deviceTypeList: [],
      deviceList: [],
      memberList: [],
      url: {
        add: '/goods/bisGoodsInfor/add',
        edit: '/goods/bisGoodsInfor/edit',
        importExcelUrl: '/sys/common/upload',
      },
      disable: false,
      limitCycledisable: false,
      list: [],
      allChildKind: [],
      allowMemberType: '',
      allowDevice: '',
      allowDeviceType: '',
      goodsPriceRule: {
        isGoodsPriceRule: 0,
        coinRule: 1,
        lotteryRule: 1,
        integralRule: 1,
        pointsRule: 1,
        alotteryRule: 1,
      },
      levelList: [],
      elevation: [],
      chargeAfter: false,
      isContinue: true,
      renewalList: [],
      renewalAfter: false,
      taxSetting: localStorage.getItem('taxSetting')
    }
  },
  mounted() {},
  methods: {
    changePrice() {
      let that = this
      that.changeSingleCoin()
      if (that.goodsPriceRule.isGoodsPriceRule == 1) {
        that.$set(that.dataForm, 'coin', that.dataForm.price * that.goodsPriceRule.coinRule)
        that.$set(that.dataForm, 'lottery', that.dataForm.price * that.goodsPriceRule.lotteryRule)
        that.$set(that.dataForm, 'integral', that.dataForm.price * that.goodsPriceRule.integralRule)
        that.$set(that.dataForm, 'points', that.dataForm.price * that.goodsPriceRule.pointsRule)
        that.$set(that.dataForm, 'alottery', that.dataForm.price * that.goodsPriceRule.alotteryRule)
      }
      if(that.taxSetting == 2){
        that.$set(that.dataForm, 'taxPrice', (that.dataForm.price - (that.dataForm.price / ( 1 + localStorage.getItem('taxRate') / 100))).toFixed(2))
      }
    },
    changeSingleCoin() {
      let realCoin = this.dataForm.realCoin ? Number(this.dataForm.realCoin) : 0
      let presentCoin = this.dataForm.presentCoin ? Number(this.dataForm.presentCoin) : 0
      this.dataForm.singleCoin =
        realCoin + presentCoin
          ? this.dataForm.price
            ? (this.dataForm.price / (realCoin + presentCoin)).toFixed(2)
            : '0.00'
          : '0.00'
    },
    getInfo() {
      let that = this
      getAction('/bis/bisSysParameter/list', { name: 'isGoodsPriceRule' }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            that.goodsPriceRule.isGoodsPriceRule = res.result.records[0].ivalue
            if (that.goodsPriceRule.isGoodsPriceRule == 1) {
              getAction('/bis/bisSysParameter/list', {
                name: 'coinRule,lotteryRule,integralRule,pointsRule,alotteryRule',
              }).then((res) => {
                if (res.success) {
                  if (res.result.total > 0) {
                    res.result.records.forEach((el) => {
                      if (el.name == 'coinRule') {
                        that.goodsPriceRule.coinRule = el.dvalue
                      }
                      if (el.name == 'lotteryRule') {
                        that.goodsPriceRule.lotteryRule = el.dvalue
                      }
                      if (el.name == 'integralRule') {
                        that.goodsPriceRule.integralRule = el.dvalue
                      }
                      if (el.name == 'pointsRule') {
                        that.goodsPriceRule.pointsRule = el.dvalue
                      }
                      if (el.name == 'alotteryRule') {
                        that.goodsPriceRule.alotteryRule = el.dvalue
                      }
                    })
                  }
                } else {
                  that.$message.error(res.message)
                }
              })
            }
          }
        } else {
          that.$message.error(res.message)
        }
      })
    },
    add() {
      this.isContinue = localStorage.getItem('goodsAddInfo')
        ? JSON.parse(localStorage.getItem('goodsAddInfo')).state
        : true
      this.dataForm = {
        code: '',
        name: '',
        warnNum: 0,
        limitCycle: '0',
        verifyType: '1',
        cost: 0,
        allowMemberDiscounts: '',
        allowMemberDiscount: 'Y',
        allowMemberType: '',
        allowLotterys: '',
        allowPointss: '',
        allowIntegrals: '',
        allowAlotterys: '',
        allowSale: 'Y',
        authSale: '',
        authSales: false,
        state: 'Y',
        allowDevices: [],
        allowDevice: '',
        allowDeviceTypes: [],
        allowDeviceType: '',
        form: 2,
        presentCoin: '0',
        realCoin: '0',
        species: '1',
        freeWithRecharge: false
      }
      this.chargeAfter = false
      this.elevation = [
        {
          memberType: undefined,
          memberTypeAfter: undefined,
          disable: false,
        },
      ]
      this.renewalAfter = false
      this.renewalList = [
        {
          type: '0',
          renewal: '',
        },
      ]
      this.visible = true
      this.edit(this.dataForm)
    },
    edit(record, state) {
      this.getInfo()
      this.taxSetting = localStorage.getItem('taxSetting')
      this.disable = state
      this.getMemberList()
      this.getDeviceTypeList()
      this.select()
      initDictOptions('bis_goods_infor.verify_type').then((res) => {
        if (res.success) {
          this.list = res.result
        }
      })
      this.visible = true
      if (this.disable == true || record.limitCycle == '0') {
        this.limitCycledisable = true
      }
      if (record.allowMemberDiscount == 'Y') {
        record.allowMemberDiscounts = true
      } else {
        record.allowMemberDiscounts = false
      }
      if (record.allowSale == 'Y') {
        record.allowSales = true
      } else {
        record.allowSales = false
      }
      if (record.allowLottery == 'Y') {
        record.allowLotterys = true
      } else {
        record.allowLotterys = false
      }
      if (record.allowPoints == 'Y') {
        record.allowPointss = true
      } else {
        record.allowPointss = false
      }
      if (record.allowIntegral == 'Y') {
        record.allowIntegrals = true
      } else {
        record.allowIntegrals = false
      }
      if (record.allowAlottery == 'Y') {
        record.allowAlotterys = true
      } else {
        record.allowAlotterys = false
      }
      if (record.authSale == '1') {
        record.authSales = true
      } else {
        record.authSales = false
      }

      this.chargeAfter = record.allowPromote ? true : false
      this.renewalAfter = record.allowRenewal ? true : false
      this.getLevels()
      this.allowMemberType = record.allowMemberType
      if (this.allowMemberType == '' || this.allowMemberType == null || this.allowMemberType == undefined) {
        this.memberShow = false
      } else {
        record.allowMemberTypes = record.allowMemberType.split(',')
        this.allowMemberType = 'part'
        this.memberShow = true
      }

      this.allowDeviceType = record.allowDeviceType
      if (this.allowDeviceType == null || this.allowDeviceType == undefined) {
        this.allowDeviceType = ''
      }
      if (this.allowDeviceType == '') {
        this.deviceTypeShow = false
      } else {
        record.allowDeviceTypes = record.allowDeviceType.split(',')
        this.allowDeviceType = 'part'
        this.deviceTypeShow = true
      }

      this.allowDevice = record.allowDevice
      if (this.allowDevice == '' || this.allowDevice == null || this.allowDevice == undefined) {
        this.deviceShow = false
      } else {
        record.allowDevices = record.allowDevice.split(',')
        this.allowDevice = 'part'
        this.deviceShow = true
      }
      let realCoin = record.realCoin ? Number(record.realCoin) : 0
      let presentCoin = record.presentCoin ? Number(record.presentCoin) : 0
      record.singleCoin = realCoin + presentCoin ? (record.price / (record.realCoin + record.presentCoin)).toFixed(2) : '0.00'
      this.dataForm = Object.assign({}, record)
      if (this.dataForm.id) {
        this.title = this.$t('编辑')
        this.getGoodsMemberTypeList(this.dataForm.id)
        this.getRenewal()
      } else {
        this.title = this.$t('新增')
        let param = {
          pageNo: 1,
          pageSize: 1,
          column: 'sort',
          order: 'asc',
          code: 'HYCB',
        }
        getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
          if (res.success) {
            if (res.result.records.length > 0) {
              this.$set(this.dataForm, 'kind', res.result.records[0].id)
            }
          }
        })
      }
      this.getDeviceList()
    },
    getRenewal(){
      getAction('/goods/bisGoodsRenewal/list', { goodsId: this.dataForm.id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            this.renewalList = res.result.records
          } else {
            this.renewalList = [
              {
                type: '0',
                renewal: '',
              },
            ]
          }
        }
      })
    },
    filterTreeNode(inputValue, treeNode) {
      // 假设输入格式为 "级别:关键词" 如 "2:电器"
      const match = inputValue.match(/([123]):(.+)/)
      if (match) {
        const level = parseInt(match[1])
        const keyword = match[2]
        const nodeLevel = this.getNodeLevel(treeNode)
        
        if (nodeLevel !== level) return false
        return treeNode.data.props.title.includes(keyword)
      }
      
      // 默认搜索
      return treeNode.data.props.title.includes(inputValue)
    },
    getNodeLevel(node) {
      let level = 1
      let parent = node.parent
      while (parent) {
        level++
        parent = parent.parent
      }
      return level
    },
    select() {
      let that = this
      let param = {}
      param.pageNo = 1
      param.pageSize = 9999
      param.column = 'sort'
      param.order = 'asc'
      getAction('/goods/bisGoodsKind/rootList', param).then((res) => {
        if (res.success) {
          that.allChildKind = []
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.id = '' + col.id
              col.pid = '' + col.pid
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            that.treeData = that.getDataByResult(res.result.records)
          } else {
            that.treeData = []
          }
        } else {
          that.$alert(res.message, this.$t('提示'), {
            confirmButtonText: this.$t('确定'),
            callback: (action) => {},
          })
        }
      })
    },
    getHasChild(record) {
      let that = this
      let query = {}
      query.pageNo = 1
      query.pageSize = 9999
      query.pid = record.id
      getAction('/goods/bisGoodsKind/rootList', query).then((res) => {
        if (res.success) {
          if (res.result.records.length > 0) {
            res.result.records.forEach((col) => {
              col.key = '' + col.id
              col.value = '' + col.id
              col.title = '' + col.name
              col.pid = '' + col.pid
              col.id = '' + col.id
              col.hasChild = '' + col.hasChild
              if (col.hasChild != '0') {
                that.getHasChild(col)
              }
            })
            record.children = that.getDataByResult(res.result.records)
            that.treeData = [...that.treeData]
          }
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    getDataByResult(result) {
      let that = this
      return result.map((item) => {
        if (item.hasChild == 0 && item.key != '0') {
          that.allChildKind.push(item.key)
        }
        return item
      })
    },
    handleGoodsSearch(value, record) {
      this.getGoodsListTwo('', value)
    },
    getTypeValue(e) {
      if (e.target.value == 'part') {
        this.memberShow = true
      } else {
        this.memberShow = false
      }
    },
    getDeviceTypeValue(e) {
      if (e.target.value == 'part') {
        this.deviceTypeShow = true
      } else {
        this.deviceTypeShow = false
      }
    },
    getDeviceValue(e) {
      if (e.target.value == 'part') {
        this.deviceShow = true
      } else {
        this.deviceShow = false
      }
    },
    getMemberList() {
      getAction('/member/bisMemberRule/list').then((res) => {
        if (res.success) {
          this.memberList = res.result.records
        }
      })
    },
    getDeviceTypeList() {
      getAction('/device/bisDeviceInfo/getTypeList', { kind: 0 }).then((res) => {
        if (res.success) {
          this.deviceTypeList = res.result.records
        }
      })
    },
    getDeviceList() {
      let that = this
      let kind = 0
      let type = ''
      if (that.allowDeviceType == 'part') {
        if (that.dataForm.allowDeviceTypes) {
          type = that.dataForm.allowDeviceTypes.join(',')
        }
      }
      getAction('/device/bisDeviceInfo/nameList', {
        pageSize: 999,
        type: type,
        kind: kind,
        column: 'type',
        order: 'ASC',
      }).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
        }
      })
    },
    changelimitCycleState(e) {
      if (e == '0') {
        this.limitCycledisable = true
      } else {
        this.limitCycledisable = false
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      let that = this
      let querydata = {}
      that.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (that.dataForm.name == '') {
            that.$message.warn(that.$t('请输入商品名称'))
            return
          }
          if (that.dataForm.allowMemberDiscounts == true) {
            that.dataForm.allowMemberDiscount = 'Y'
          } else {
            that.dataForm.allowMemberDiscount = 'N'
          }
          if (that.dataForm.allowSales == true) {
            that.dataForm.allowSale = 'Y'
            if (that.dataForm.price == undefined || that.dataForm.price === '') {
              that.$message.warn(that.$t('售价不能为空'))
              return
            }
          } else {
            that.dataForm.allowSale = 'N'
          }
          if (that.dataForm.allowLotterys == true) {
            that.dataForm.allowLottery = 'Y'
            if (that.dataForm.lottery == undefined) {
              that.$message.warn(that.$t('彩票值不能为空'))
              return
            }
          } else {
            that.dataForm.allowLottery = 'N'
          }
          if (that.dataForm.allowPointss == true) {
            that.dataForm.allowPoints = 'Y'
            if (that.dataForm.points == undefined) {
              that.$message.warn(that.$t('A币不能为空'))
              return
            }
          } else {
            that.dataForm.allowPoints = 'N'
          }
          if (that.dataForm.allowIntegrals == true) {
            that.dataForm.allowIntegral = 'Y'
            if (that.dataForm.integral == undefined) {
              that.$message.warn(that.$t('积分不能为空'))
              return
            }
          } else {
            that.dataForm.allowIntegral = 'N'
          }
          if (that.dataForm.allowAlotterys == true) {
            that.dataForm.allowAlottery = 'Y'
            if (that.dataForm.alottery == undefined) {
              that.$message.warn(that.$t('A票值不能为空'))
              return
            }
          } else {
            that.dataForm.allowAlottery = 'N'
          }
          if (that.dataForm.authSales == true) {
            that.dataForm.authSale = 1
          } else {
            that.dataForm.authSale = 0
          }
          if (that.allowMemberType == 'part') {
            if (that.dataForm.allowMemberTypes == null || that.dataForm.allowMemberTypes == '') {
              that.$message.warn(that.$t('请选择会员类型'))
              return
            }
            that.dataForm.allowMemberType = that.dataForm.allowMemberTypes.join(',')
          } else {
            that.dataForm.allowMemberType = ''
          }
          if (that.allowDeviceType == 'part') {
            if (that.dataForm.allowDeviceTypes == null || that.dataForm.allowDeviceTypes == '') {
              that.$message.warn(that.$t('请选择适用终端'))
              return
            }
            that.dataForm.allowDeviceType = that.dataForm.allowDeviceTypes.join(',')
          } else {
            that.dataForm.allowDeviceType = ''
          }
          if (that.allowDevice == 'part') {
            if (that.dataForm.allowDevices == null || that.dataForm.allowDevices == '') {
              that.$message.warn(that.$t('请选择适用终端'))
              return
            }
            that.dataForm.allowDevice = that.dataForm.allowDevices.join(',')
          } else {
            that.dataForm.allowDevice = ''
          }
          if (that.chargeAfter) {
            that.dataForm.allowPromote = 1
            let arr = that.elevation.filter((el) => {
              return el.disable
            })
            if (arr.length == 0) {
              that.$message.warn(that.$t('至少设置一条晋级规则'))
              return
            }
          } else {
            that.dataForm.allowPromote = 0
          }
          if (that.renewalAfter) {
            that.dataForm.allowRenewal = 1
            let arr = that.renewalList.filter((el) => {
              return el.renewal===''
            })
            if (arr.length > 0) {
              that.$message.warn(that.$t('请设置续期规则'))
              return
            }
            that.dataForm.renewalList=that.renewalList
          } else {
            that.dataForm.allowRenewal = 0
          }
          querydata = that.dataForm
          let arr = that.elevation.filter((el) => {
            return el.disable
          })
          querydata.bisGoodsMemberTypeList = arr
          let httpurl = ''
          let method = ''
          if (!that.dataForm.id) {
            httpurl += that.url.add
            method = 'post'
          } else {
            httpurl += that.url.edit
            method = 'put'
          }
          that.confirmLoading = true
          httpAction(httpurl, querydata, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
                if (that.dataForm.id || !that.isContinue) {
                  that.close()
                } else {
                  that.initDataForm()
                }
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    getGoodsMemberTypeList(id) {
      this.elevation = []
      getAction('/goods/bisGoodsMemberType/list', { goodsId: id }).then((res) => {
        if (res.success) {
          if (res.result.total > 0) {
            res.result.records.forEach((rs) => {
              rs.disable = true
            })
            this.elevation = res.result.records
          } else {
            this.elevation = [
              {
                memberType: undefined,
                memberTypeAfter: undefined,
                disable: false,
              },
            ]
          }
        }
      })
    },
    getLevels() {
      this.levelList = []
      getAction('/member/bisMemberRule/list', { notNonmember: 1, manageState: 'Y', pageSize: 40 }).then((res) => {
        if (res.success) {
          this.levelList = res.result.records
          return
        }
      })
    },
    changeChargeStatue(e) {
      this.chargeAfter = e.target.checked
    },
    changeRenewalStatue(e) {
      this.renewalAfter = e.target.checked
    },
    addElevation() {
      this.elevation.push({
        memberType: undefined,
        memberTypeAfter: undefined,
        disable: false,
      })
    },
    deleteElevation(i) {
      this.elevation.splice(i, 1)
    },
    editElevation(i) {
      this.$set(this.elevation[i], 'disable', false)
    },
    saveElevation(item, i) {
      if (item.memberType && item.memberTypeAfter) {
        this.$set(this.elevation[i], 'disable', true)
      } else if (!item.memberType) {
        this.$message.warn(this.$t('请选择晋级前会员类型'))
      } else {
        this.$message.warn(this.$t('请选择晋级后会员类型'))
      }
    },
    changeAddState() {
      localStorage.setItem('goodsAddInfo', JSON.stringify({ state: this.isContinue }))
    },
    initDataForm() {
      this.dataForm = {
        code: '',
        name: '',
        warnNum: 0,
        limitCycle: '0',
        verifyType: '1',
        cost: 0,
        allowMemberDiscounts: '',
        allowMemberDiscount: 'Y',
        allowMemberType: '',
        allowLotterys: '',
        allowPointss: '',
        allowIntegrals: '',
        allowAlotterys: '',
        allowSale: 'Y',
        authSale: '',
        authSales: false,
        state: 'Y',
        allowDevices: [],
        allowDevice: '',
        allowDeviceTypes: [],
        allowDeviceType: '',
        form: 2,
        presentCoin: '0',
        realCoin: '0',
        species: '1',
        freeWithRecharge: false
      }
      this.chargeAfter = false
      this.elevation = [
        {
          memberType: undefined,
          memberTypeAfter: undefined,
          disable: false,
        },
      ]
      this.renewalAfter = false
      this.renewalList = [
        {
          type: '0',
          renewal: '',
        },
      ]
      this.$refs.code.focus()
      this.edit(this.dataForm)
    },
    changeSelectState(e, key, type){
      if(type == 1){
        this.$set(this.dataForm, key, e.target.checked ? 1 : 0)
      }
      if(type == 2){
        this.$set(this.dataForm, key, e.target.checked ? 'Y' : 'N')
      }
    },
  },
}
</script>
<style scoped>
</style>